package com.example.servicetest

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.util.AttributeSet
import android.view.View

class DuffXfermodeView: View {

    val paint = Paint()

    constructor(context: Context) : this(context,null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //设置背景色
        canvas.drawARGB(255, 139, 197, 186)

        val layerId = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null)

        val canvasWidth: Int = width
        val r = canvasWidth / 3f
        //绘制黄色的圆形
        paint.color = -0x33bc
        canvas.drawCircle(r, r, r, paint)
        //绘制蓝色的矩形
        paint.color = -0x995501
        paint.xfermode =  PorterDuffXfermode(PorterDuff.Mode.CLEAR);
        canvas.drawRect(r, r, r * 2.7f, r * 2.7f, paint)
        canvas.restoreToCount(layerId);

    }

}